import { apiOne, apiTwo } from "./api";
import * as echarts from "./echarts.min.js";

// 用柱状图展示2020年各地区所有订单消费额的中位数
//（降序排列，若有小数则四舍五入保留两位）
const barOne = async () => {
  let diquobj = {};
  let diquarr = [];
  let diquname = [];
  let diqudata = [];
  let data = (await apiOne()).data.data;
  data.forEach((element) => {
    diquobj[element.regionName] = [];
  });
  data.forEach((element) => {
    diquobj[element.regionName].push(element.finalTotalAmount);
  });
  for (const key in diquobj) {
    diquobj[key] = diquobj[key].sort((a, b) => {
      return b - a;
    });
    if (diquobj[key].length % 2 == 1) {
      diquobj[key] = diquobj[key][diquobj[key].length / 2 - 0.5];
    } else {
      diquobj[key] =
        (diquobj[key][diquobj[key].length / 2] +
          diquobj[key][diquobj[key].length / 2 - 1]) /
        2;
    }
    diquarr.push({ name: key, data: diquobj[key] });
  }
  diquarr = diquarr.sort((a, b) => {
    return b.data - a.data;
  });
  diquarr.forEach((element) => {
    diquname.push(element.name);
    diqudata.push(element.data);
  });
  console.log(diquname, diqudata);
  let myEcharts = echarts.init(document.getElementById("mainBox"));
  myEcharts.setOption({
    title: {
      text: "2020年各地区所有订单消费额的中位数",
    },
    xAxis: {
      name: "地区",
      data: diquname,
    },
    yAxis: {
      name: "订单消费额",
    },
    series: [
      {
        type: "bar",
        data: diqudata,
      },
    ],
  });
};

//用饼状图展示2020年各地区的消费总额占比
const pieOne = async () => {
  let diquobj = {};
  let diquarr = [];
  let diquname = [];
  let diqudata = [];
  let datasum = [];
  let data = (await apiOne()).data.data;
  data.forEach((element) => {
    diquobj[element.regionName] = 0;
  });
  data.forEach((element) => {
    diquobj[element.regionName] =
      diquobj[element.regionName] + element.finalTotalAmount;
  });
  for (const key in diquobj) {
    diquarr.push({ name: key, data: diquobj[key] });
  }
  diquarr = diquarr.sort((a, b) => b.data - a.data);
  diquarr.forEach((element) => {
    diquname.push(element.name);
    diqudata.push(element.data);
    datasum.push({ name: element.name, value: element.data });
  });
  console.log(diquname, diqudata);

  let myEcharts = echarts.init(document.getElementById("mainBox"));
  console.log(myEcharts);
  myEcharts.setOption({
    title: {
      text: "2020年各地区的消费总额占比",
    },
    toolbar: {},
    series: [
      {
        type: "pie",
        data: datasum,
        label: {
          formatter: "{b} {d}%",
        },
      },
    ],
  });
};

//用基础散点图展示每年上架商品数量的变化情况
const scatter = async () => {
  let yename = [];
  let yedatae = [];
  let dataobj = {};
  let dataarr = [];
  let data = (await apiTwo()).data.data;
  // console.log(data)
  data.forEach((element) => {
    dataobj[element.year] = 0;
  });
  data.forEach((element) => {
    dataobj[element.year] = dataobj[element.year] + 1;
  });
  for (const key in dataobj) {
    yename.push(key);
    yedatae.push(dataobj[key]);
    dataarr.push({ name: key, data: dataobj[key] });
  }
  console.log(yename, yedatae);

  let myEcharts = echarts.init(document.getElementById("mainBox"));
  myEcharts.setOption({
    title: {
      text: "每年上架商品数量的变化情况",
    },
    xAxis: {
      name: "年份",
      data: yename,
    },
    yAxis: {
      name: "商品上架数量",
    },
    series: [
      {
        type: "scatter",
        data: yedatae,
      },
    ],
  });
};

//条形图展示2020年平均消费额
//（四舍五入保留两位小数）最高的5个省份
const barTwo = async () => {
  let datanaem = [];
  let datadate = [];
  let dataobj = {};
  let dataobjindex = {};
  let dataarr = [];
  let data = (await apiOne()).data.data;
  // console.log(data)
  data.forEach((element) => {
    dataobj[element.provinceName] = 0;
    dataobjindex[element.provinceName] = 0;
  });
  // console.log(dataobj)
  data.forEach((element) => {
    dataobj[element.provinceName] =
      dataobj[element.provinceName] + element.finalTotalAmount;
    dataobjindex[element.provinceName] = dataobjindex[element.provinceName] + 1;
  });
  for (const key in dataobj) {
    let linsidata = (dataobj[key] / dataobjindex[key]).toFixed(2);
    dataarr.push({ name: key, data: linsidata });
  }
  dataarr = dataarr.sort((a, b) => {
    return b.data - a.data;
  });
  // console.log(dataarr)
  dataarr = dataarr.splice(0, 5);
  dataarr.forEach((e) => {
    datanaem.push(e.name);
    datadate.push(e.data);
  });
  console.log(datanaem, datadate);
  let myEcharts = echarts.init(document.getElementById("mainBox"));
  myEcharts.setOption({
    title: {
      text: "2020年平均消费额",
    },
    xAxis: {
      name: "平均消费额",
    },
    yAxis: {
      name: "省份",
      data: datanaem,
    },
    series: [
      {
        type: "bar",
        data: datadate,
      },
    ],
  });
};

/*
折柱混合图展示2020年各省份平均消费额
（四舍五入保留两位小数）
和地区平均消费额
（四舍五入保留两位小数）的对比情况，
柱状图展示平均消费额最高的5个省份
折线图展示这5个省所在的地区的平均消费额变化
*/
const hunhe = async () => {
  let datanaem = [];
  let datadate = [];
  let dataobj = {};
  let dataobjindex = {};
  let dataarr = [];
  let diquobj = {};
  let diquobjindex = {};
  let diquarr = [];
  let diquname = [];
  let diqudate = [];
  let datasum = {};
  let data = (await apiOne()).data.data;
  // console.log(data)
  // console.log(data)
  data.forEach((element) => {
    dataobj[element.provinceName] = 0;
    dataobjindex[element.provinceName] = 0;
    diquobj[element.regionName] = 0;
    diquobjindex[element.regionName] = 0;
    datasum[element.provinceName] = element.regionName;
  });
  // console.log(dataobj)
  data.forEach((element) => {
    dataobj[element.provinceName] =
      dataobj[element.provinceName] + element.finalTotalAmount;
    dataobjindex[element.provinceName] = dataobjindex[element.provinceName] + 1;
    diquobj[element.regionName] =
      diquobj[element.regionName] + element.finalTotalAmount;
    diquobjindex[element.regionName] = diquobjindex[element.regionName] + 1;
  });
  for (const key in dataobj) {
    let linsidata = (dataobj[key] / dataobjindex[key]).toFixed(2);
    dataarr.push({ name: key, data: linsidata });
  }
  for (const key in diquobj) {
    let linsidata = (diquobj[key] / diquobjindex[key]).toFixed(2);
    diquarr.push({ name: key, data: linsidata });
  }
  dataarr = dataarr
    .sort((a, b) => {
      return b.data - a.data;
    })
    .splice(0, 5);

  dataarr.forEach((e) => {
    datanaem.push(e.name);
    datadate.push(e.data);
  });
  datanaem.forEach((element) => {
    for (const key in datasum) {
      if (key == element) {
        // console.log(element)
        diquname.push(datasum[key]);
        diquarr.forEach((e1) => {
            if(e1.name == datasum[key]){
                diqudate.push(e1.data)
            }
        });
      }
    }
  });

//   console.log(datasum);
  console.log(datanaem, datadate);
  console.log(diquname, diqudate);

  let myEcharts = echarts.init(document.getElementById("mainBox"))
  myEcharts.setOption({
      title:{
          text:"2020年各省份平均消费额和地区平均消费额"
      },
      xAxis:[
        {
            name:"省份",
            data:datanaem
        },
        {
            name:"地区",
            diquname
        }
      ],
      yAxis:{
          name:"平均消费额"
      },
      series:[
          {
              type:"bar",
              data:datadate
          },
          {
            type:"line",
            data:diqudate   
          }
      ]
  })
};
export const mainFn = () => {
  hunhe();
};
